package com.syedu.cels.util;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.util.Properties;

public class BeanFactory {
    private static Properties prop;

    static {
        try {
            prop = new Properties();
            InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("bean.properties");
            prop.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Object getBean(String beanName) {
        Object obj = null;
        String className = prop.getProperty(beanName);
        try {
            //反射技术
            //1.获取类的Class实例
            Class<?> c = Class.forName(className);
            //2.动态获取类的构造方法
            Constructor<?> constructor = c.getConstructor();
            //3.动态调用构造方法
            obj = constructor.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return obj;
    }
}